/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import MODEL.Mesa;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Rodrigo
 */
public class DaoMesa extends Banco{
    
    public DaoMesa(){
        connection=Conecta();
    }
    
    public List<Mesa> getMesas(){
        
        List<Mesa> mesa = new ArrayList<Mesa>();
        try {    
            Statement stm = connection.createStatement();
            String SQL = "SELECT * FROM mesa";  
            ResultSet rs = stm.executeQuery(SQL);  

            while(rs.next())  
            {  
               Mesa m = new Mesa();
               m.setCodigo(rs.getInt("cod_mesa"));
               m.setDescricao(rs.getString("descricao"));
               mesa.add(m);
               
            } 
            
        } catch (Exception ex) {
            System.out.println(ex);
        }
       
        return mesa;
    }
   
    public void insereMesa(Mesa mesa){
        try {    
            String SQL = "INSERT INTO mesa VALUES (default,?)";
            PreparedStatement stmt = connection.prepareStatement(SQL);  
            stmt.setString(1, mesa.getDescricao());
            stmt.execute();  
            stmt.close();  
            System.out.println("Gravado!"); 
            
        } catch (Exception ex) {
            System.out.println(ex);
        }finally{
            Fecha();
        }
    }
    
    public Mesa getMesa(int cod){
        Mesa m = new Mesa();
        try{
            Statement stm = connection.createStatement();
            String SQL = "SELECT * FROM mesa WHERE cod_mesa="+cod;  
            ResultSet rs = stm.executeQuery(SQL);  

            while(rs.next())  
            {    
               m.setCodigo(rs.getInt("cod_mesa"));
               m.setDescricao(rs.getString("descricao"));  
            } 
        }catch(Exception e){
            
        }finally{
            Fecha();
        }
        return m;
    }
}
